package org.kernelab.numeric.apps;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import org.kernelab.basis.Tools;
import org.kernelab.basis.gui.JTextAreaOutputStream;

public class AppParser extends JFrame
{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 6828358934016790209L;

	private static final void configLookAndFeel()
	{
		Tools.configLookAndFeel();

		// UIManager.put("Button.font", Game.FONT);
		// UIManager.put("Button.margin", new Insets(0, 0, 0, 0));
		UIManager.put("TextArea.font", new Font("Arial", Font.PLAIN, 12));
		// UIManager.put("Label.font", Game.FONT);
		// UIManager.put("TabbedPane.font", Game.FONT);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		new AppParser();
	}

	// private Launcher launcher;

	private Parser		parser;

	private JTextArea	output;

	private JTextArea	input;

	private PrintStream	outputStream;

	public AppParser()
	{
		super();

		AppParser.configLookAndFeel();

		// this.launcher = new Launcher(this);

		this.setParser(new Parser());

		this.setInput(new JTextArea());
		this.setOutput(new JTextArea());
		this.setOutputStream(new JTextAreaOutputStream(this.getOutput(),
				JTextAreaOutputStream.APPEND).getPrintStream());

		Tools.getOuts().add(this.getOutputStream());

		this.arrange();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Parser");
		this.setVisible(true);

		// this.launcher.close();

		this.getInput().grabFocus();
	}

	private void arrange()
	{
		this.getOutput().setEditable(false);

		this.getOutput().setTabSize(4);

		this.getInput().addKeyListener(new KeyListener() {

			@Override
			public void keyPressed(KeyEvent e)
			{

			}

			@Override
			public void keyReleased(KeyEvent e)
			{

			}

			@Override
			public void keyTyped(KeyEvent e)
			{
				if ((int) e.getKeyChar() == 10
						&& e.getModifiersEx() == InputEvent.CTRL_DOWN_MASK)
				{
					try {
						submit();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}
		});

		JScrollPane outputPanel = new JScrollPane(this.getOutput());
		JScrollPane inputPanel = new JScrollPane(this.getInput());

		outputPanel
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		outputPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

		JTabbedPane outputPane = new JTabbedPane();
		JTabbedPane inputPane = new JTabbedPane();

		outputPane.add("输出", outputPanel);
		inputPane.add("输入", inputPanel);

		this.setLayout(new GridBagLayout());

		final GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.gridx = 0;
		gbc.gridy = 0;

		this.add(outputPane, gbc);

		gbc.gridy++;
		gbc.weighty = 0.2;

		this.add(inputPane, gbc);

		this.setPreferredSize(new Dimension(500, 600));

		this.pack();
	}

	public JTextArea getInput()
	{
		return input;
	}

	public JTextArea getOutput()
	{
		return output;
	}

	public PrintStream getOutputStream()
	{
		return outputStream;
	}

	public Parser getParser()
	{
		return parser;
	}

	public void setInput(JTextArea input)
	{
		this.input = input;
	}

	public void setOutput(JTextArea output)
	{
		this.output = output;
	}

	public void setOutputStream(PrintStream outputStream)
	{
		this.outputStream = outputStream;
	}

	public void setParser(Parser parser)
	{
		this.parser = parser;
	}

	private void submit()
	{
		String string = this.getInput().getText();

		this.getParser().parse(string);

		this.getInput().setText("");

		this.getInput().grabFocus();
	}

}
